package com.be.beadmin.system.impl.ProjectSysServiceImpl;

import com.be.beadmin.basicData.mapper.UserMapper;
import com.be.beadmin.basicData.service.UserService;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.material.entity.PickingTask;
import com.be.beadmin.material.service.PickingTaskService;
import com.be.beadmin.product.entity.WorkOrderInfor;
import com.be.beadmin.product.repository.WorkOrderInforRepository;
import com.be.beadmin.product.service.WorkOrderInforService;
import com.be.beadmin.project.entity.Info;
import com.be.beadmin.project.entity.MasterPlan;
import com.be.beadmin.project.mapper.BatchProductMapper;
import com.be.beadmin.project.mapper.InfoMapper;
import com.be.beadmin.project.mapper.InfoMapperEx;
import com.be.beadmin.project.mapper.PayInfoMapperEx;
import com.be.beadmin.project.repository.*;
import com.be.beadmin.project.service.*;
import com.be.beadmin.service.constant.UserConst;
import com.be.beadmin.service.entity.NotifyInfo;
import com.be.beadmin.service.impl.MinioService;
import com.be.beadmin.service.service.SerialNumberService;
import com.be.beadmin.system.service.ProjectSysService.InfoSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 9:44 2023/8/31
 * @Modified By:
 **/
@Service
public class InfoSysServiceImpl implements InfoSysService {
    @Autowired
    private InfoRepository infoRepository;

    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private UserService userService;

    @Autowired
    private BatchProductService batchProductService;

    @Autowired
    private ProductInfoRepository productInfoRepository;
    @Autowired
    private MasterPlanRepository masterPlanRepository;
    @Autowired
    private BatchPlanRepository batchPlanRepository;


    @Autowired
    private PlanStageRepository planStageRepository;


    @Autowired
    private WorkOrderInforService workOrderInforService;

    @Autowired
    private WorkOrderInforRepository workOrderInforRepository;
    @Autowired
    private PickingTaskService pickingTaskService;

    /**
     * 删除指定项目
     *
     * @param request
     * @param id
     */
    @Transactional
    @Override
    public void delInfo(HttpServletRequest request, String id) {
        if(!userService.isRole(request, UserConst.ADMINROLECODE)) throw new BeadminException("不是系统管理不允许删除！！");

        // 1. 找到该项目
        Info info = infoRepository.findInfoById(id);
        // 删除项目下的所有东西
        MasterPlan masterPlan = masterPlanRepository.findMasterPlanByInfoIdAndDelete(id, 0);
        List<String> batchPlanIdList = batchPlanRepository.findBatchPlanIdByMasterPlanIdAndDelete(masterPlan.getId(), 0);
        // 删除该项目的所有流水编码
        serialNumberService.deleteSerialNumberByInfo(info.getId());

        // 删除批次柜体
        batchProductService.deleteBatchProductAndRelationInfo(info);


        // 删除批次计划
        batchPlanRepository.deleteBatchPlanByInfoIdAndDelete(1, info.getId(), 0);
        // 删除主计划
        masterPlanRepository.deleteMasterPlanByInfoIdAndDelete(1, info.getId(), 0);
        // 删除导入的柜体信息
        productInfoRepository.deleteProductInfoByInfoIdAndDelete(1, info.getId(), 0);
        // 删除所有计划的阶段
        planStageRepository.deletePlanStageByPlanIdAndDelete(1,masterPlan.getId(),0);
        planStageRepository.deletePlanStageByPlanIdInAndDelete(1,batchPlanIdList,0);
        // 删除所有工单
        workOrderInforService.deleteWoByInfo(info.getId());
        // 删除所有分料信息
        pickingTaskService.deletePickingTask(info.getId());
        // 删除所有客户投诉信息
        // 删除所有接线错误信息
        // 删除所有工序信息
        // 删除所有的检验单信息
        // 删除所有产能统计信息

        // 2. 将项目的删除字段设置1， 表示已删除
        info.setDelete(1);
        // 3. 保存到数据库中
        infoRepository.save(info);
    }

    /**
     * 关闭项目
     *
     * @param infoId
     * @return
     */
    @Override
    public String shutProject(String infoId) {
        Info info = infoRepository.findInfoByIdAndDelete(infoId, 0);
        if (info == null) {
            throw new BeadminException("未找到该项目，或项目已被删除");
        }
        if (info.getContractState().equals("已关闭")) {
            return "项目已关闭";
        }

        // 有工单未关闭，不能关闭项目
        StringBuilder sb = new StringBuilder("工单:【");
        List<String> wo = workOrderInforRepository.findWoByInfoIdAndNotStateAndDelete(infoId, 2, 0);
        if (!wo.isEmpty()) {
            for (int i = 0; i < wo.size(); i++) {
                sb.append(wo.get(i));
                if (i!=wo.size()-1) {
                    sb.append(",");
                } else {
                    sb.append("】还未关闭，不能结束项目");
                }
            }

            throw new BeadminException(sb.toString());
        }

        info.setContractState("已关闭");
        infoRepository.save(info);
        return "项目关闭成功";
    }
}
